/*
 * @Author: your name
 * @Date: 2022-02-23 19:55:12
 * @LastEditTime: 2022-04-26 17:54:06
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \miniprogram-1\pages\repair\repairRecords\repairRecords.js
 */
const {waitDistributeTaskList,delegateTasks,stopRepair,queryEmployeeList} = require('../../../utils/api')
Page({
  data: {
    noId:'',
    active: 'a',
    pickType:1,
    taskList:[{
      id:'',
      no: '',
      creator:'',
      level: '',
      type:'',
      repairItemName:'',
      createTime: '',
      community:{
        name:'',
      },
      createTime:'',
      remark:'',
      resourcesList:[]
    }],
    columns:[],
    rangeList:[
      '全部',
      '低',
      '中',
      '高'
    ],
    level:'',
    responser:"",
    currentPage: 1,
    empList:[]
  },
      /**
   * 生命周期函数--监听页面显示
   */
   onShow: function () {
    this.searchList()
  },
  searchList(){
    let type = this.data.active =='a'?0:1
    let level = this.data.rangeList.indexOf(this.data.level)-1
    if(level<0){
      level = ''
    }
    waitDistributeTaskList({type,level}).then(res=>{
      this.setData({
        taskList: res.data
      })
    })
  },
  onChangeTab(e){
    this.setData({
     active: e.detail.name
    })
    this.searchList()
  },
  onClosePick(){
    this.setData({
      showPicker: false
    })
  },
  onReachBottom(){
    this.setData({
      currentPage: this.data.currentPage+1
    })
    // const tasks =  this.data.taskList.concat([this.data.taskList[0]])
    // this.setData({
    //   taskList: tasks
    // })
  },
  clickToPick(){
    this.setData({
      showPicker: true,
      pickType:1,
      columns: this.data.rangeList 
    })
  },
  toDis(e){
    this.setData({
      showPicker:true,
      pickType: 2
    })
    let communityId = e.currentTarget.dataset.comid
    let noId = e.currentTarget.dataset.id
    this.setData({
      noId
    })
    queryEmployeeList(communityId).then(res=>{
      if(res.state == 200){
        this.setData({
          empList: res.data,
          columns: res.data.map(e=> e.userName)
        })
      }
    })
  },
  confirmDis(e){
    this.setData({
      showPicker:false
    })
    if(this.data.pickType == 1){
      this.setData({
        level: e.detail.value
      })
      this.searchList()
    }else {
      this.setData({
        responser: this.data.empList.find(el=> el.userName===e.detail.value).id
      })
      this.distributeTask()
    }
  },
  distributeTask(){
    delegateTasks({id: this.data.noId, userId: this.data.responser}).then(res=>{
      if(res.state == 200){
        wx.showToast({
          title: '派单成功',
          icon: 'none',
        });
        this.searchList()
      }
    })
  },
  zhongzhi(e){
    const no = e.currentTarget.dataset.no
    stopRepair({no}).then(res=>{
      if(res.state == 200){
        wx.showToast({
          title: '操作成功',
          icon: 'none',
        });
        this.searchList()
      }
    })
  },
  goback(){
    wx.navigateBack({
      delta: 1
    });
      
  }
})
